<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/engine/start.php');

include_once(dirname(dirname(__FILE__)) . '/_init.php');

$user = get_user_by_username('greengar');
login($user);

$start = get_input('start');
$end = get_input('end');

//$filename = "places";

//$jsons = file($filename);

//SAMPLE CODE
//$username = 'sco';
//fa574894bddc43aa96c556eb457b4009
//$api_key = 'f6cd524ac9c4413abfb41d7123757d9';
$api_key = 'fa574894bddc43aa96c556eb457b4009';
//$checkin_num = 5;
$lat = 10.802478;
$lng = 106.70722;
$radius = 50;
$url = "http://api.gowalla.com/spots?lat=$lat&lng=$lng&radius=$radius";
//echo $url;

// setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array (
    "Accept: application/json",
    "X-Gowalla-API-Key: {$api_key}",
));
$body = curl_exec($ch);
curl_close($ch);
//echo 'body_start*' . $body . '*body_end';
//die($body);
/*
$json = json_decode($body, true);

foreach($json['stamps'] as $stamp) {
    print $stamp['spot']['name'] . '<br/>';
    print "<pre>";
    print_r($stamp);
    print "</pre>";
}
 */
//END SAMPLE CODE
$json_body = json_decode($body, TRUE);
$jsons = $json_body['spots'];
//die(json_encode($jsons));
$mocks = array();
//echo 6;
$count = 0;

foreach ($jsons as $json) {
    echo $count . '<br>';
    //echo json_encode($json);
    if($count < $start) {
        $count++;
        continue;
    }
    
    if($count > $end) {
        break;
    }
    //echo $json;
    //echo '1';
    $json_place = $json;
    if(is_array($json_place) && $json_place['name']) {
        $name = $json_place['name'];
        $lat = $json_place['lat'];
        $long = $json_place['lng'];
        $category = $json_place['spot_categories']['name'];
        $image_url = $json_place['image_url'];
        $mocks[$name] = array($category, $image_url, $lat, $long);
    }
    
    $count++;
}

//die();

$count = $start;

foreach($mocks as $key => $value) {
    echo $count . '<br>';
    if($count < $start) {
        $count++;
        continue;
    }
    
    if($count > $end) {
        break;
    }
    
    $name = $key;
    $annotation = array(
        "category" => $value[0],
        "image_url" => $value[1]
    );
    $annotation = json_encode($annotation);
    $rating = 2;
    $interest = array(
        INTEREST_NAME => $name,
        INTEREST_ANNOTATION => $annotation,
        INTEREST_RATING_INTEREST_RATING => $rating);

    $ratingEntity = greengar_opinions_add_interest($interest, get_loggedin_userid());
    if($ratingEntity) {
        $rating = $ratingEntity->toArray();
        $interest = $rating['interest'];
        $interestId = $interest['guid'];
        $interestEntity = get_entity($interestId);
        $interestEntity->saveLocation($value[2], $value[3]);
        echo json_encode($ratingEntity->toArray()) . '<br>';
    } else {
        echo "Fail " . json_encode($interest) . '<br>';
    }
    
    $count++;
}

?>
